﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Threading;

namespace SLControlLibrary.Resources.Controls.Calendar
{
    public class ucContentControl : ContentControl
    {
        #region Variables
        private DispatcherTimer DoubleClickTimer = new DispatcherTimer();
        private Drag_ResizeManager manager = null;
        #endregion

        #region Event Variables
        public delegate void MouseDblClickHandler(object sender, MouseButtonEventArgs e);
        public event MouseDblClickHandler MouseDoubleClick;
        public event EventHandler Base_Resized;
        #endregion

        #region Constrcuctor
        public ucContentControl()
        {
            DoubleClickTimer.Interval = new TimeSpan(0, 0, 0, 0, 300);
            DoubleClickTimer.Tick += new EventHandler(DoubleClickTimer_Tick);
            base.MouseLeftButtonDown += new MouseButtonEventHandler(BaseContentControl_MouseLeftButtonDown);
            base.MouseLeftButtonUp += new MouseButtonEventHandler(BaseContentControl_MouseLeftButtonUp);
            base.MouseMove += new MouseEventHandler(BaseContentControl_MouseMove);
        }
        #endregion

        #region Methods
        #region SetDraging
        public void SetDraging(Panel _Panel, bool CanResizeX, bool CanResizeY)
        {
            manager = new Drag_ResizeManager(_Panel, CanResizeX, CanResizeY);
            manager.AddDragResizeItem(this);
        }
        #endregion

        public void Resize(Size NewSize)
        {
            if (Base_Resized != null)
                Base_Resized(NewSize, null);
        }
        #endregion

        #region Events
        #region DoubleClickTimer_Tick
        private void DoubleClickTimer_Tick(object sender, EventArgs e)
        {
            DoubleClickTimer.Stop();
        }
        #endregion

        #region BaseContentControl_MouseLeftButtonDown
        private void BaseContentControl_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            if (DoubleClickTimer.IsEnabled)
            {
                DoubleClickTimer.Stop();

                if (MouseDoubleClick != null)
                    MouseDoubleClick(sender, e);
            }
            else
            {
                DoubleClickTimer.Start();
            }

            base.OnMouseLeftButtonDown(e);
        }
        #endregion

        #region BaseContentControl_MouseLeftButtonUp
        private void BaseContentControl_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            base.ReleaseMouseCapture();
            base.OnMouseLeftButtonUp(e);
        }
        #endregion

        #region BaseContentControl_MouseMove
        private void BaseContentControl_MouseMove(object sender, MouseEventArgs e)
        {            
            base.OnMouseMove(e);
        }
        #endregion
        #endregion
    }
}
